<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Spinatiev Ivan
 * Date: 9/27/12
 * Time: 2:59 PM
 * To change this template use File | Settings | File Templates.
 */

class Page_Be_CleanUpDataBase extends Page
{
	/**
	 * @var Page_Be_CleanUpDataBase
	 */
	protected $tableObject;

	function __construct() {
		$this->classTableObject = 'Db_CleanUpDataBase';
		parent::__construct();
	}

	public function defaultAction() {
		$allDataTables = Db_CleanUpDataBase::getTablesWithFieldDeleted();
		self::$TPL->assign('allDataTables', $allDataTables);
		self::$TPL->appendJsFile('admin/data_base');
		self::$TPL->appendCssFile('admin/clean_up_data_base');
		self::$TPL->setBodyTemplate('list_data_base');
		return array(
			'type' => 'display'
		);
	}

	public function cleanUpAction() {
		$deletedRowsString = "";
		foreach (post('name') AS $key => $value) {
			if ($value == 1) {
				$tablesNameArray[]= $key;
			}
		}
		if ($tablesNameArray) {
            $DeletedRows = Db_CleanUpDataBase::deleteDeletedRows($tablesNameArray);
			if (!$DeletedRows) {
				$this->error = true;
				$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
			} else {
				foreach ($DeletedRows as $tableName => $numberRows) {
					$deletedRowsString .= "<br />".$tableName." : ".$numberRows." rows, ";
				}
				$deletedRowsString = substr($deletedRowsString, 0, -2);
				$this->message = t(T_FORM_MSG, 'success').$deletedRowsString;
			}
		}
		return array(
			'type' => 'redirect',
			'query' => false
		);
	}
}

